package com.kt.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.kt.mes.domain.ProductLine;
import com.kt.mes.domain.ProlSiteSet;
import com.kt.mes.mapper.ProductLineMapper;
import com.kt.mes.mapper.ProlSiteSetMapper;
import com.kt.mes.mapper.SiteMapper;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kt.mes.mapper.ProlSiteMapper;
import com.kt.mes.domain.ProlSite;
import com.kt.mes.service.IProlSiteService;
import com.kt.mes.domain.TreeSelectPro;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产线工位Service业务层处理
 * 
 * @author lzc
 * @date 2021-01-09
 */
@Service
public class ProlSiteServiceImpl implements IProlSiteService 
{
    @Autowired
    private ProlSiteMapper prolSiteMapper;

    @Autowired
    private ProductLineMapper productLineMapper;

    @Autowired
    private SiteMapper siteMapper;

    @Autowired
    private ProlSiteSetMapper prolSiteSetMapper;
    /**
     * 查询产线工位
     * 
     * @param id 产线工位ID
     * @return 产线工位
     */
    @Override
    public ProlSite selectProlSiteById(Long id)
    {


        return prolSiteMapper.selectProlSiteById(id);
    }

    /**
     * 查询产线工位列表
     * 
     * @param prolSite 产线工位
     * @return 产线工位
     */
    @Override
    public List<ProlSite> selectProlSiteList(ProlSite prolSite)
    {
        return prolSiteMapper.selectProlSiteList(prolSite);
    }

    /**
     * 新增产线工位
     * 
     * @param prolSite 产线工位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProlSite(ProlSite prolSite)
    {
        //先增产线工位
        prolSite.setCreateTime(DateUtils.getNowDate());
        int flag=  prolSiteMapper.insertProlSite(prolSite);

        //同时新增设置
        ProlSiteSet prolSiteSet =new ProlSiteSet();
        prolSiteSet.setProlSiteId(prolSite.getId());
        prolSiteSetMapper.insertProlSiteSet(prolSiteSet);


        return flag;
    }

    /**
     * 修改产线工位
     * 
     * @param prolSite 产线工位
     * @return 结果
     */
    @Override
    public int updateProlSite(ProlSite prolSite)
    {
        prolSite.setUpdateTime(DateUtils.getNowDate());
        return prolSiteMapper.updateProlSite(prolSite);
    }

    /**
     * 批量删除产线工位
     * 
     * @param ids 需要删除的产线工位ID
     * @return 结果
     */
    @Override
    public int deleteProlSiteByIds(Long[] ids)
    {
        return prolSiteMapper.deleteProlSiteByIds(ids);
    }

    /**
     * 删除产线工位信息
     * 
     * @param id 产线工位ID
     * @return 结果
     */
    @Override
    public int deleteProlSiteById(Long id)
    {
        return prolSiteMapper.deleteProlSiteById(id);
    }


    //创建树形结构图
    @Override
    public List<TreeSelectPro> buildProlSiteTreeSelect() {
        List<TreeSelectPro>  returnList=new ArrayList<>();
        //一级目录为产线
        List<ProductLine> list_proline=  productLineMapper.selectProductLineList(null);

        for(ProductLine proLine:list_proline){
            TreeSelectPro  rootTree =new TreeSelectPro();
            rootTree.setId(proLine.getPlId());
            rootTree.setLabel(proLine.getPlId()+"-"+proLine.getPlName());
            rootTree.setLv("1");
            //二级目录为工位
            ProlSite prolSiteParam=new ProlSite();
            prolSiteParam.setPlId(proLine.getPlId());
            List<ProlSite>  list_prolSite= prolSiteMapper.selectProlSiteList(prolSiteParam);


            if(list_prolSite.isEmpty()==false){
                List<TreeSelectPro> sonList=new ArrayList<>();

                for(ProlSite pls:list_prolSite){
                    TreeSelectPro sonTree=new TreeSelectPro();
                        sonTree.setId(pls.getId()+"");
                        sonTree.setLabel(pls.getNum()+"-"+pls.getSiteName());
                        sonTree.setLv("2");
                        sonList.add(sonTree);
                }
                rootTree.setChildren(sonList);
            }

            returnList.add(rootTree);
        }




        return returnList;
    }


}
